package com.flong.lambda;

import com.flong.DataUtils;
import com.flong.vo.Person;
import com.flong.vo.Student;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author:liangjl
 * @Date: 2020/6/21-17:51
 * @Eamil:jilongliang@sina.com
 * @Description:
 * 1、partitioningBy 分区
 * 2、joining加入
 */
public class Test9 {

    @Test
    public void partitioningBy() {
        Stream<Student> studentStream = DataUtils.studentStream();

        //通过分数进行分区判断，false与true，就两个组
        Map<Boolean, List<Student>> listMap = studentStream.collect(Collectors.partitioningBy(s -> {
            return s.getSocre() > 60;
        }));
        listMap.forEach((key, val) -> {
            System.out.println("key=" + key + ",value=" + val);
        });
        //输出的结果：
        //key=false,value=[Student(userName=小龙女, sex=女, age=60, socre=50.0)]
        //key=true,value=[Student(userName=周伯通, sex=男, age=100, socre=80.0), Student(userName=瑛姑, sex=女, age=89, socre=90.0), Student(userName=一灯大师, sex=男, age=120, socre=120.0), Student(userName=杨过, sex=男, age=47, socre=100.0)]

    }

    @Test
    public void joining() {
        Stream<Student> studentStream = DataUtils.studentStream();
        //String names = studentStream.map(Student::getUserName).collect(Collectors.joining(","));
        //System.out.println(names);
        //输出结果：周伯通,瑛姑,一灯大师,小龙女,杨过

        //,表示用,进行分割，prefix前缀,suffix后缀
        String names = studentStream.map(Student::getUserName).collect(Collectors.joining(",","prefix","suffix"));
        System.out.println(names);
        //输出结果：prefix周伯通,瑛姑,一灯大师,小龙女,杨过suffix
    }


}
